﻿using System;
using System.Text;
using EPiServer.Core;
using EPiCode.GoogleMapsLocation;
using EPiServer.Labs.MobilePack.Site;
using System.Globalization;

namespace EPiServer.Labs.MobilePack.Templates.Location
{
    public partial class MapListing : MobileBase
    {
        public PageDataCollection Pages { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.BindDefaultGoogleInformation();        
        }

        public void BindDefaultGoogleInformation()
        {
            Pages = GetChildren(CurrentPageLink);
            pagelist.DataSource = Pages;
            pagelist.DataBind();   
        }

        public string GetFriendlyURL(PageReference pageLink, string URL)
        {
            var url = new UrlBuilder(URL);
            Global.UrlRewriteProvider.ConvertToExternal(url, pageLink, Encoding.UTF8);
            return url.Uri.ToString();
        }

        public string StartPosition
        {
            get
            {
                GoogleMapsLocation gml = CurrentPage["StartPosition"] as GoogleMapsLocation;
                if (gml != null)
                {
                    return gml.Latitude + "," + gml.Longitude;
                }
                
                return "59.3426606750, 18.0736160278";
            }
        }

        public string ZoomLevel
        {
            get
            {
                var level = CurrentPage["ZoomLevel"] as string;
                if (level != null)
                {
                    return level;
                }
                
                return string.Empty;
            }
        }

        public string Positions
        {
            get
            {
                var result = new StringBuilder();

                foreach (PageData pd in Pages)
                {
                    GoogleMapsLocation gml = pd["LocationInfo"] as GoogleMapsLocation;
                    
                    string scriptString = @" $('#map_canvas_" + CurrentPage.PageLink.ID + @"').gmap('addMarker', { 'title': '" + pd.PageName + @"', 'bound': true, 'position': new google.maps.LatLng(" + gml.Latitude.ToString(CultureInfo.InvariantCulture) + @", " + gml.Longitude.ToString(CultureInfo.InvariantCulture) + @") }, function (map, marker) {
									        $('#map_canvas_" + CurrentPage.PageLink.ID + @"').gmap('addInfoWindow', { 'position': marker.getPosition(), 
                                            'content': '<a data-role="+"\"button\" href=\"" + GetFriendlyURL(pd.PageLink, pd.LinkURL) + "\" style=\"color:black;  font-size: 12px;line-height:1px; text-decoration: none; text-shadow:0 0px\">" + pd.PageName + "</a>" + @"' }, function (iw) {
									            $(marker).click(function () {
									                iw.open(map, marker);
									            });
									        });
									        
									    });";
                    result.Append(scriptString);
                 }

 

                return result.ToString();
            }
        }
    }
}
